// index.js
// const app = getApp()
const { envList } = require('../../envList.js');

Page({
  data: {
    scenicName: "",
    imageSrc: "",
    scenicPrice: 0,
    scenicRePrice: 0,
    totalNumber: 1,
    totalPrice: 0
  },
  // onLoad(options) {
  //   console.log(options);
  //   const {
  //     scenicName,
  //     scenicDes,
  //     imageSrc,
  //     price,
  //     computedPrice
  //   } = options
  //   this.setData({
  //     scenicName,
  //     scenicDes,
  //     imageSrc,
  //     price,
  //     computedPrice
  //   })
  // },
  onReady () {
    const pageThis = this
    this.getOpenerEventChannel().on('sendTicketData', (data) => {
      if(data.value) {
        console.log(data.value);
        const { scenicName, imageSrc, scenicPrice, scenicRePrice } = data.value
        pageThis.setData({ scenicName, imageSrc, scenicPrice, scenicRePrice, totalPrice: scenicPrice * 1 })
      }
    })
  },
  onClickPowerInfo(e) {
    const index = e.currentTarget.dataset.index;
    const powerList = this.data.powerList;
    powerList[index].showItem = !powerList[index].showItem;
    if (powerList[index].title === '数据库' && !this.data.haveCreateCollection) {
      this.onClickDatabase(powerList);
    } else {
      this.setData({
        powerList
      });
    }
  },

  onChangeShowEnvChoose() {
    wx.showActionSheet({
      itemList: this.data.envList.map(i => i.alias),
      success: (res) => {
        this.onChangeSelectedEnv(res.tapIndex);
      },
      fail (res) {
        console.log(res.errMsg);
      }
    });
  },

  onChangeSelectedEnv(index) {
    if (this.data.selectedEnv.envId === this.data.envList[index].envId) {
      return;
    }
    const powerList = this.data.powerList;
    powerList.forEach(i => {
      i.showItem = false;
    });
    this.setData({
      selectedEnv: this.data.envList[index],
      powerList,
      haveCreateCollection: false
    });
  },

  onClickDatabase(powerList) {
    wx.showLoading({
      title: '',
    });
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      config: {
        env: this.data.selectedEnv.envId
      },
      data: {
        type: 'createCollection'
      }
    }).then((resp) => {
      if (resp.result.success) {
        this.setData({
          haveCreateCollection: true
        });
      }
      this.setData({
        powerList
      });
      wx.hideLoading();
    }).catch((e) => {
      console.log(e);
      this.setData({
        showUploadTip: true
      });
      wx.hideLoading();
    });
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
            select: 3
        })
    }
  },

});
